<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/3/23 0023
 * Time: 下午 2:40
 */

namespace app\admin\model;


use think\facade\Cookie;
use think\Model;

class ArticleModel extends Model
{
    protected $table = "article";
    //自动获取时间戳
    protected $autoWriteTimestamp = true;

    /**
     * 与文章附表 一对一
     * @return \think\model\relation\HasOne
     */
    public function articleInfo()
    {
        return $this->hasOne("ArticleInfoModel","article_id","id");
    }

    /**
     * 与音乐表 一对多
     * @return \think\model\relation\HasMany
     */
    public function music()
    {
        return $this->hasMany("MusicModel","id","music_id");
    }

    /**
     * 与轮播图 一对一
     * @return \think\model\relation\HasOne
     */
    public function banner()
    {
        return $this->hasOne("BannerModel","article_id","id");
    }

    public function user()
    {
        return $this->hasOne("UserModel","id","user_id");
    }

    /**
     * 获取所有 新闻 文章
     * @return array
     * @throws \think\exception\DbException
     */
    static public function getArticleAll()
    {
        $model = new self();
        $result = $model->where("status","=",1)
            ->order("create_time","DESC")
            ->paginate()
            ->toArray();
        return is_null($result) ? [] : $result;
    }

    /**
     * 验证文章是否是 该用户的
     * @param $id
     * @return bool|string
     */
    static public function validateArticle($id)
    {
        $userId = Cookie::get("userId");
        $result = self::get($id);
        if (isset($result["id"])) {
            return $result["user_id"] == $userId ? true : "该文章";
        } else {
            return "文章不存在";
        }
    }

    static public function getArticleFind($id)
    {
        $result = self::get($id);
        return $result;
    }

    /**
     * 删除新闻文章   以及所有有关附表 轮播图
     * @param $id
     * @return bool|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    static public function delArticle($id)
    {
        $model = new self();
        $model->startTrans();

        $result = $model->where("id","=",$id)->delete();
        if ($result < 0) {
            $model->rollback();
            return "删除失败";
        }

        $resultInfo = $model->articleInfo()->where("article_id","=",$id)->delete();
        if ($resultInfo < 0) {
            $model->rollback();
            return "删除失败";
        }

        $resultBanner = $model->banner()->where("article_id","=",$id)->delete();
        if ($resultBanner < 0) {
            $model->rollback();
            return "删除失败";
        }

        $model->commit();

        return true;
    }

    /**
     *  修改文章
     * @param $data  // 修改数据
     * @return bool|string
     * @throws \think\exception\PDOException
     */
    static public function editArticle($data)
    {
        $model = new self();
        $model->startTrans();

        $result = $model->allowField(true)->save($data,["id" => $data["id"]]);
        if (!$result) {
            $model->rollback();
            return "数据修改失败";
        }

        $dataInfo = [
            "article_id" => $data["id"],
            "detail" => $data["detail"],
            "headImgSrc" => $data["imgSrc"]
        ];
        $resultInfo = ArticleInfoModel::editDetail($dataInfo);
        if (!$resultInfo) {
            $model->rollback();
            return "数据修改失败";
        }

        $dataBanner =  [
            "article_id" => $data["id"],
            "bannerImg" => $data["imgSrc"]
        ];
        $resultBanner = BannerModel::editBanner($dataBanner);
        if (!$resultBanner) {
            $model->rollback();
            return "数据修改失败";
        }

        $model->commit();

        return true;
    }

    /**
     * 修改状态
     * @param $id
     * @param $status
     * @return bool
     */
    static public function updateStatus($id,$status)
    {
        $model = new self();
        $temp = [
            "status" => $status == 1 ? 0 : 1
        ];
        $result = $model->save($temp,["id" => $id]);
        return $result;
    }


    /**
     * 增加 新闻     新闻附表  轮播图
     * @param $data
     * @return bool|string
     * @throws \think\exception\PDOException
     */
    static public function addArticle($data) {
        $model = new self();
        $model->startTrans();

        $result = $model->allowField(true)->save($data);
        if ($result < 0) {
            $model->rollback();
            return "数据上传失败";
        }

        $articleId = $model->getLastInsID();

        $dataInfo = [
            "article_id" => $articleId,
            "detail" => $data["detail"],
            "headImgSrc" => $data["imgSrc"]
        ];
        $resultInfo = ArticleInfoModel::addDetail($dataInfo);
        if ($resultInfo < 0) {
            $model->rollback();
            return "数据上传失败";
        }

        $dataBanner =  ["article_id" => $articleId,"bannerImg" => $data["imgSrc"],'sort' => 99];
        $resultBanner = BannerModel::addBanner($dataBanner);
        if ($resultBanner < 0) {
            $model->rollback();
            return "数据上传失败";
        }

        $model->commit();

        return true;
    }
}